#include <iostream>
#include <cstring>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

enum
{
    OK = 0,
    PipeError,
    ForkError
};

int main()
{
    int fds[2] = {0};

    int n = pipe(fds);
    if (n != 0)
    {
        std::cerr << "pipe erroor" << std::endl;
        return PipeError;
    }

    pid_t id = fork();
    if (id < 0)
    {
        std::cerr << "fork error" << std::endl;
        return ForkError;
    }
    else if (id == 0)
    {
        ::close(fds[1]);

        char buffer[1024];
        ssize_t n = ::read(fds[0], buffer, 1024);
        buffer[n] = 0;

        std::cout << buffer << std::endl;

        ::exit(0);
    }
    else
    {
        ::close(fds[0]);

        std::string message = "i am father";
        ::write(fds[1], message.c_str(), message.size());

        ::close(fds[1]);
        pid_t rid = waitpid(id, nullptr, 0);
        std::cout << "waitchild success..." << std::endl;
    }

    return 0;
}